package mortgage;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;

import utils.ApplicationUtilities;

import com.opensymphony.xwork2.ActionSupport;

import connection.MyConnection;

public class CalculateMortgageInterestAction extends ActionSupport {

	 private int id;
	 private String finalDate;
	 private double interestAmount;
	 private long noOfDays;
	 private double totalAmount;
	 private double givenAmount;
	 
	@Override
	public String execute() throws Exception {
	
		 
		MyConnection mc = new MyConnection();
		mc.connect();
		Date  mortgageDate=null;
		double rate=0.0;
		double givenamount=0.0;
	    PreparedStatement ps=mc.con.prepareStatement("SELECT * FROM mortgageentry WHERE id=?");
	    ps.setInt(1, getId());
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			mortgageDate = rs.getDate("mdate");
			rate=rs.getDouble("rateofinterest");
			givenamount=rs.getDouble("givenamount");
		}
           SimpleDateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy");
           
            
		
		
		 Date finalDateTemp=new Date(dateFormat.parse(finalDate).getTime());
		 
		  System.out.println(finalDate);
		  System.out.println(finalDateTemp.toString());
		  System.out.println(mortgageDate);
		 
		 
	 	 noOfDays=ApplicationUtilities.findNoOfDays(mortgageDate,finalDateTemp);
	 	 System.out.println(noOfDays);
		  setNoOfDays(noOfDays);
		  
            double p,r,t;
             r=rate/30;
            interestAmount=givenamount*r*noOfDays/100;
            setInterestAmount(interestAmount);
		    totalAmount=givenamount+getInterestAmount();
		    setTotalAmount(totalAmount);
		
		    
		    setGivenAmount(givenamount);
		return SUCCESS;
	}

	public int getId() {
		return id;
	}

	

	public String getFinalDate() {
		return finalDate;
	}

	public void setFinalDate(String finalDate) {
		this.finalDate = finalDate;
	}

	public double getInterestAmount() {
		return interestAmount;
	}

	public long getNoOfDays() {
		return noOfDays;
	}

	public double getTotalAmount() {
		return totalAmount;
	}

	public void setId(int id) {
		this.id = id;
	}

	
	public void setInterestAmount(double interestAmount) {
		this.interestAmount = interestAmount;
	}

	public void setNoOfDays(long noOfDays) {
		this.noOfDays = noOfDays;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}

	public double getGivenAmount() {
		return givenAmount;
	}

	public void setGivenAmount(double givenAmount) {
		this.givenAmount = givenAmount;
	}
	
	
	
}
